﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Noris.Tools.TraceVisualiser.Support
{
    /// <summary>
    /// Zajistí dialog pro výběr barvy
    /// </summary>
    public static class DialogColor
    {
        #region Color dialog
        /// <summary>
        /// Otevře ColorDialog, s přednastavenou barvou a persistovanými CustomColors.
        /// Pokud uživatel vybere a potvrdí barvu, metoda vrací true a v proměnné value bude nově vybraná barva.
        /// Pokud uživatel nic nevybere, metoda vrací false a hodnota value se nezmění.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SelectColor(ref Color value)
        {
            bool result = false;
            using (ColorDialog cd = new ColorDialog())
            {
                cd.AllowFullOpen = true;
                cd.AnyColor = true;
                cd.Color = value;
                cd.CustomColors = Steward.CustomColors;
                bool fullOpen = true;
                if (IsColorBasicDialog(value) || IsColorInList(value, cd.CustomColors, true))
                    fullOpen = false;
                cd.FullOpen = fullOpen;
                cd.ShowHelp = false;
                cd.SolidColorOnly = false;

                var resultDialog = cd.ShowDialog();
                if (resultDialog == DialogResult.OK)
                {
                    value = cd.Color;
                    Steward.CustomColors = cd.CustomColors;
                    result = true;
                }
            }
            return result;
        }
        #endregion
        #region Detekce Basic barev
        public static bool IsColorBasicDialog(Color color)
        {
            return IsColorInList(color, ColorDialogBasicColors);
        }
        public static bool IsColorInList(Color color, IEnumerable<Int32> colorValues, bool convertCOMcolor)
        {
            if (colorValues == null) return false;
            List<Color> colors = new List<System.Drawing.Color>();
            foreach (Int32 value in colorValues)
            {
                Color c = Color.FromArgb(value);
                if (convertCOMcolor) c = Color.FromArgb(c.B, c.G, c.R);             // Specialita: ignorovat hodnotu A, prohodit hodnoty R <=> B, ponechat hodnotu G
                colors.Add(c);
            }
            return IsColorInList(color, colors);
        }
        public static bool IsColorInList(Color color, IEnumerable<Color> colors)
        {
            if (colors == null) return false;
            return colors.Count(c => (c.A == color.A && c.R == color.R && c.G == color.G && c.B == color.B)) > 0;
        }
        /// <summary>
        /// Obsahuje barvy, které se v dialogu ColorDialog zobrazují jako základní
        /// </summary>
        public static Color[] ColorDialogBasicColors
        {
            get
            {
                #region Soupis barev, které se v dialogu ColorDialog zobrazují jako základní
                Color[] basics = new System.Drawing.Color[] {
Color.FromArgb(0xFF, 0xFF, 0x80, 0x80)   /* ffff8080 */ ,
Color.FromArgb(0xFF, 0xFF, 0xFF, 0x80)   /* ffffff80 */ ,
Color.FromArgb(0xFF, 0x80, 0xFF, 0x80)   /* ff80ff80 */ ,
Color.FromArgb(0xFF, 0x00, 0xFF, 0x80)   /* ff00ff80 */ ,
Color.FromArgb(0xFF, 0x80, 0xFF, 0xFF)   /* ff80ffff */ ,
Color.FromArgb(0xFF, 0x00, 0x80, 0xFF)   /* ff0080ff */ ,
Color.FromArgb(0xFF, 0xFF, 0x80, 0xC0)   /* ffff80c0 */ ,
Color.FromArgb(0xFF, 0xFF, 0x80, 0xFF)   /* ffff80ff */ ,
Color.FromArgb(0xFF, 0xFF, 0x00, 0x00)   /* Red */ ,
Color.FromArgb(0xFF, 0xFF, 0xFF, 0x00)   /* Yellow */ ,
Color.FromArgb(0xFF, 0x80, 0xFF, 0x00)   /* ff80ff00 */ ,
Color.FromArgb(0xFF, 0x00, 0xFF, 0x40)   /* ff00ff40 */ ,
Color.FromArgb(0xFF, 0x00, 0xFF, 0xFF)   /* Aqua */ ,
Color.FromArgb(0xFF, 0x00, 0x80, 0xC0)   /* ff0080c0 */ ,
Color.FromArgb(0xFF, 0x80, 0x80, 0xC0)   /* ff8080c0 */ ,
Color.FromArgb(0xFF, 0xFF, 0x00, 0xFF)   /* Fuchsia */ ,
Color.FromArgb(0xFF, 0x80, 0x40, 0x40)   /* ff804040 */ ,
Color.FromArgb(0xFF, 0xFF, 0x80, 0x40)   /* ffff8040 */ ,
Color.FromArgb(0xFF, 0x00, 0xFF, 0x00)   /* Lime */ ,
Color.FromArgb(0xFF, 0x00, 0x80, 0x80)   /* Teal */ ,
Color.FromArgb(0xFF, 0x00, 0x40, 0x80)   /* ff004080 */ ,
Color.FromArgb(0xFF, 0x80, 0x80, 0xFF)   /* ff8080ff */ ,
Color.FromArgb(0xFF, 0x80, 0x00, 0x40)   /* ff800040 */ ,
Color.FromArgb(0xFF, 0xFF, 0x00, 0x80)   /* ffff0080 */ ,
Color.FromArgb(0xFF, 0x80, 0x00, 0x00)   /* Maroon */ ,
Color.FromArgb(0xFF, 0xFF, 0x80, 0x00)   /* ffff8000 */ ,
Color.FromArgb(0xFF, 0x00, 0x80, 0x00)   /* Green */ ,
Color.FromArgb(0xFF, 0x00, 0x80, 0x40)   /* ff008040 */ ,
Color.FromArgb(0xFF, 0x00, 0x00, 0xFF)   /* Blue */ ,
Color.FromArgb(0xFF, 0x00, 0x00, 0xA0)   /* ff0000a0 */ ,
Color.FromArgb(0xFF, 0x80, 0x00, 0x80)   /* Purple */ ,
Color.FromArgb(0xFF, 0x80, 0x00, 0xFF)   /* ff8000ff */ ,
Color.FromArgb(0xFF, 0x40, 0x00, 0x00)   /* ff400000 */ ,
Color.FromArgb(0xFF, 0x80, 0x40, 0x00)   /* ff804000 */ ,
Color.FromArgb(0xFF, 0x00, 0x40, 0x00)   /* ff004000 */ ,
Color.FromArgb(0xFF, 0x00, 0x40, 0x40)   /* ff004040 */ ,
Color.FromArgb(0xFF, 0x00, 0x00, 0x80)   /* Navy */ ,
Color.FromArgb(0xFF, 0x00, 0x00, 0x40)   /* ff000040 */ ,
Color.FromArgb(0xFF, 0x40, 0x00, 0x40)   /* ff400040 */ ,
Color.FromArgb(0xFF, 0x40, 0x00, 0x80)   /* ff400080 */ ,
Color.FromArgb(0xFF, 0x00, 0x00, 0x00)   /* Black */ ,
Color.FromArgb(0xFF, 0x80, 0x80, 0x00)   /* Olive */ ,
Color.FromArgb(0xFF, 0x80, 0x80, 0x40)   /* ff808040 */ ,
Color.FromArgb(0xFF, 0x80, 0x80, 0x80)   /* Gray */ ,
Color.FromArgb(0xFF, 0x40, 0x80, 0x80)   /* ff408080 */ ,
Color.FromArgb(0xFF, 0xC0, 0xC0, 0xC0)   /* Silver */ ,
Color.FromArgb(0xFF, 0x40, 0x00, 0x40)   /* ff400040 */ ,
Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF)   /* White */
                
            };
                #endregion
                return basics;
            }
        }
        #endregion
    }
}
